package cn.bulibuqi.myblog.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author bulibuqi
 * 拦截检查
 *  1配置好拦截器要拦截那些请求
 *  2 把这些配置放在容器中
 *  3 在mvc的配置类中注册
 *
 * @create 2021-12-21:13
 */

public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 功能描述: <br>
     * 〈目标方法执行之前〉
     * @Param: [request, response, handler]
     * @Return: boolean true 放行 false 拦截
     * @Author: bulibuqi
     * @Date: 2021/12/15 21:18
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        if(request.getSession().getAttribute("user")==null){
            //重定向到登录界面
            response.sendRedirect("/admin");
            return false;
        }
        return true;
    }

    /**
     * 功能描述: <br>
     * 〈目标方法执行之后〉
     * @Param: [request, response, handler, modelAndView]
     * @Return: void
     * @Author: bulibuqi
     * @Date: 2021/12/15 21:18
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 功能描述: <br>
     * 〈页面渲染之后〉
     * @Param: [request, response, handler, ex]
     * @Return: void
     * @Author: bulibuqi
     * @Date: 2021/12/15 21:19
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
